### 4.42 `mktemp`, `mkstemp`, `mkostemp`, `mkstemps`,

Synopsis

         #include <stdlib.h>
         char *mktemp(char *path);
         char *mkdtemp(char *path);
         int mkstemp(char *path);
         int mkstemps(char *path, int suffixlen);
         int mkostemp(char *path, int flags);
         int mkostemps(char *path, int suffixlen, int flags);

         char *_mktemp_r(struct _reent *reent, char *path);
         char *_mkdtemp_r(struct _reent *reent, char *path);
         int *_mkstemp_r(struct _reent *reent, char *path);
         int *_mkstemps_r(struct _reent *reent, char *path, int len);
         int *_mkostemp_r(struct _reent *reent, char *path,
             int flags);
         int *_mkostemps_r(struct _reent *reent, char *path, int len,
             int flags);

Description
`mktemp`, `mkstemp`, and `mkstemps` attempt to generate a file name that is not yet in use for any existing file. `mkstemp` and `mkstemps` create the file and open it for reading and writing; `mktemp` simply generates the file name (making `mktemp` a security risk). `mkostemp` and `mkostemps` allow the addition of other `open` flags, such as `O_CLOEXEC`, `O_APPEND`, or `O_SYNC`. On platforms with a separate text mode, `mkstemp` forces `O_BINARY`, while `mkostemp` allows the choice between `O_BINARY`, `O_TEXT`, or 0 for default. `mkdtemp` attempts to create a directory instead of a file, with a permissions mask of 0700.

You supply a simple pattern for the generated file name, as the string at path. The pattern should be a valid filename (including path information if you wish) ending with at least six ``X`' characters. The generated filename will match the leading part of the name you supply, with the trailing ``X`' characters replaced by some combination of digits and letters. With `mkstemps`, the ``X`' characters end suffixlen bytes before the end of the string.

The alternate functions `_mktemp_r`, `_mkdtemp_r`, `_mkstemp_r`, `_mkostemp_r`, `_mkostemps_r`, and `_mkstemps_r` are reentrant versions. The extra argument reent is a pointer to a reentrancy structure.

Returns
`mktemp` returns the pointer path to the modified string representing an unused filename, unless it could not generate one, or the pattern you provided is not suitable for a filename; in that case, it returns `NULL`. Be aware that there is an inherent race between generating the name and attempting to create a file by that name; you are advised to use `O_EXCL|O_CREAT`.

`mkdtemp` returns the pointer path to the modified string if the directory was created, otherwise it returns `NULL`.

`mkstemp`, `mkstemps`, `mkostemp`, and `mkostemps` return a file descriptor to the newly created file, unless it could not generate an unused filename, or the pattern you provided is not suitable for a filename; in that case, it returns `-1`.

Portability
ANSI C does not require either `mktemp` or `mkstemp`; the System V Interface Definition requires `mktemp` as of Issue 2. POSIX 2001 requires `mkstemp`, and POSIX 2008 requires `mkdtemp` while deprecating `mktemp`. `mkstemps`, `mkostemp`, and `mkostemps` are not standardized.

Supporting OS subroutines required: `getpid`, `mkdir`, `open`, `stat`.